package com.base.cn.platform.os.common.utils.web;

import com.base.cn.platform.os.common.constants.CacheConstants;
import com.base.cn.platform.os.common.constants.CommonConstants;
import com.base.cn.platform.os.common.j2cache.CustomCacheChannel;
import com.base.cn.platform.os.common.j2cache.J2CacheRegion;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 获取页面用户登录工具
 * @author s.li
 * @date 2018/5/25 15:18
 */
@Component
public final class WebMobileLoginUtil {
    private JsonParser jsonParser = new JsonParser();
    @Autowired
    private CustomCacheChannel customCacheChannel;
    @Autowired
    private  CommonConstants  commonConstants;

    /**
     * 获取登录用户分享KEY
     * @param request  HttpServletRequest
     * @return 分享KEY
     */
    /**
     * 获取登录用户分享KEY
     * @param request  HttpServletRequest
     * @return 分享KEY
     */
    public String getShareKey(HttpServletRequest request){
        String sin = this.getSin(request);
        String shareKey = (String)customCacheChannel.get(J2CacheRegion.THIRTY_DAY_REGION.getKey(),sin+ CacheConstants.SHARE_KEY_SUFFIX).getValue();
        if(StringUtils.isEmpty(shareKey)){
            shareKey = (String)customCacheChannel.get(J2CacheRegion.ONE_DAY_REGION.getKey(),sin+CacheConstants.SHARE_KEY_SUFFIX).getValue();
        }
        return shareKey;
    }

    /**
     * 登录当前登录的用户ID
     * @param request
     * @return
     */
    public BigDecimal getLoginUserId(HttpServletRequest request){
        JsonObject userMap = this.getLoginUser(request);
        if(ObjectUtils.isNotEmpty(userMap)){
            BigDecimal userId = userMap.get("id").getAsBigDecimal();
            if(ObjectUtils.isNotEmpty(userId)){
                String _userId = userId.toString();
                if(_userId.contains(".")){
                    _userId = _userId.split("\\.")[0];
                    userId = new BigDecimal(_userId);
                }
                return userId;
            }
        }
        return new BigDecimal(0);
    }

    /**
     * 验证是否是登录
     * @param request
     * @return false未登录，true已登录
     */
    public boolean isLogin(HttpServletRequest request){
        if(ObjectUtils.isNotEmpty(this.getLoginUser(request))){
            return true;
        }
        return false;
    }

    /**
     * 登录登录用户信息
     * @param request
     * @return 用户Map<String,Object>
     */
    public JsonObject getLoginUser(HttpServletRequest request){
        String sin = getSin(request);
        /**
         * 从30天或1天的region中 获取登录用户信息
         */
        String webUserStr = (String)customCacheChannel.get(J2CacheRegion.THIRTY_DAY_REGION.getKey(), sin).getValue();
        if(StringUtils.isNotEmpty(webUserStr)){
            return jsonParser.parse(webUserStr).getAsJsonObject();
        }else{
            webUserStr = (String)customCacheChannel.get(J2CacheRegion.ONE_DAY_REGION.getKey(), sin).getValue();
            if(StringUtils.isNotEmpty(webUserStr)) return jsonParser.parse(webUserStr).getAsJsonObject();
        }
        return null;
    }

    /**
     * 获取学员昵称
     * @param request
     * @return
     */
    public String getNickname(HttpServletRequest request){
        JsonObject loginUser = getLoginUser(request);
        String nickname = null;
        if(loginUser !=null){
            nickname = loginUser.get("nickname").getAsString();
        }
        return nickname;
    }

    public String getAvatar(HttpServletRequest request){
        JsonObject loginUser = getLoginUser(request);
        String avatar = null;
        if(loginUser !=null){
            JsonElement jsonElement = loginUser.get("avatar");
            if(jsonElement !=null && !jsonElement.isJsonNull()){
                avatar = loginUser.get("avatar").getAsString();
            }else{
                String STATIC_PATH = commonConstants.get(CommonConstants.STATIC_PATH);
                StringBuilder stringBuilder = new StringBuilder("/front/web/v3/img/default/userface.gif");
                avatar = stringBuilder.insert(0,STATIC_PATH).toString();
            }
        }
        return avatar;
    }

    private String getSin(HttpServletRequest request){
        boolean isMobile = DeviceUtils.isMobileDevice(request);
        boolean isWeChat = DeviceUtils.isWeChat(request);
        String uri = request.getRequestURI();
        String sin = WebUtils.getCookie(request, CommonConstants.USER_SINGLE_ID);
        if(uri.startsWith("/mobile/") || isMobile || isWeChat){
            sin = WebUtils.getCookie(request,CommonConstants.MOBILE_USER_SINGLE_ID);
        }
        return sin;
    }

}
